package com.dex4d.ui.wallet.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.dex4d.R;
import com.dex4d.async.KeyAsyncTask;
import com.dex4d.core.AppToolBarBaseActivity;
import com.dex4d.dialog.CommonMessageDialog;
import com.dex4d.dialog.CustomDialog;
import com.dex4d.model.Mnemonic;
import com.dex4d.utils.BaseUtils;
import com.dex4d.utils.Commons;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by chengyuchun on 2018/10/20.
 */
public class VerifyMnemonicActivity extends AppToolBarBaseActivity implements Commons.BUNDLE_KEYS {

    @BindView(R.id.rv_verify_mnemonic)
    RecyclerView mRvVerifyMnemonic;
    private CommonAdapter mRvVerifyMnemonicAdpter;
    private List<Mnemonic> mVerifyMnemonicList = new ArrayList<Mnemonic>();

    @BindView(R.id.rv_mnemonic)
    RecyclerView mRvMnemonic;
    private CommonAdapter mRvMnemonicAdpter;
    private List<Mnemonic> mDataList = new ArrayList<Mnemonic>();
    private ArrayList<String> mMnemonicBackupList = new ArrayList<String>();

    @BindView(R.id.btn_finish)
    Button mBtnFinish;

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, VerifyMnemonicActivity.class);
        activity.startActivity(intent);
    }

    public static void start(Activity activity, Bundle bundle) {
        Intent intent = new Intent(activity, VerifyMnemonicActivity.class);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @Override
    protected void initToolbar() {
        showBackView();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_verify_mnemonic;
    }

    @Override
    protected void initVariables() {
        mMnemonicBackupList = (ArrayList)getIntent().getExtras().get(MNEMONIC_CODE);
        for(int i = 0;i<mMnemonicBackupList.size();i++){
            String word = mMnemonicBackupList.get(i);
            Mnemonic mnemonic = new Mnemonic(i, word);
            mDataList.add(mnemonic);
        }
        Collections.shuffle(mDataList);
    }

    @Override
    protected void loadData() {

        setRvAdapter();
        setRvVerifyAdapter();
    }

    //设置待选择的RecyclerView的apdater
    private void setRvAdapter(){
        mRvMnemonicAdpter = new CommonAdapter<Mnemonic>(this, R.layout.item_generate_mnemonic,mDataList) {
            @Override
            protected void convert(ViewHolder holder, final Mnemonic mnemonic, int position) {
                holder.setText(R.id.tv_word,mnemonic.word);
                if(mnemonic.isChecked){
                    holder.setBackgroundRes(R.id.tv_word,R.drawable.mnemonic_selected_bg);
                }else {
                    holder.setBackgroundRes(R.id.tv_word,R.drawable.mnemonic_unselected_bg);
                }

                holder.setOnClickListener(R.id.tv_word, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setSelection(mnemonic);
                        if(mVerifyMnemonicList.size()==12){
                            mBtnFinish.setTextAppearance(mContext,R.style.Theme_ButtonClickableStyle);
                            mBtnFinish.setBackgroundResource(R.drawable.btn_blue_clickable_bg);
                        }
                    }
                });
            }
        };
        mRvMnemonic.setLayoutManager(new GridLayoutManager(this, 3));
        mRvMnemonic.setAdapter(mRvMnemonicAdpter);
    }

    //设置待验证的RecyclerView的apdater
    private void setRvVerifyAdapter(){
        mRvVerifyMnemonicAdpter = new CommonAdapter<Mnemonic>(this, R.layout.item_verify_mnemonic,mVerifyMnemonicList) {
            @Override
            protected void convert(ViewHolder holder, final Mnemonic mnemonic, int position) {
                holder.setText(R.id.tv_word,mnemonic.word);

                holder.setOnClickListener(R.id.tv_word, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setUnselected(mnemonic);
                        mBtnFinish.setTextAppearance(mContext,R.style.Theme_ButtonUnClickStyle);
                        mBtnFinish.setBackgroundResource(R.drawable.btn_blue_unclick_bg);
                    }
                });
            }
        };

        FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(this);
        flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP);
        flexboxLayoutManager.setAlignItems(AlignItems.STRETCH);
        flexboxLayoutManager.setFlexDirection(FlexDirection.ROW); //默认是row

        mRvVerifyMnemonic.setLayoutManager(flexboxLayoutManager);
        mRvVerifyMnemonic.setAdapter(mRvVerifyMnemonicAdpter);
    }

    @OnClick({R.id.btn_finish})
    public void onViewClick(){
        if(mVerifyMnemonicList.size()!=12){
            return;
        }
        if(verifyMnemonic()) {
            showLoadingDialog();
            String randomPwd = BaseUtils.getRandomPwd(); //6位随机密码
            new KeyAsyncTask(randomPwd,mMnemonicBackupList,true,this).execute(); //创建钱包
        }else {
            CommonMessageDialog dialog = new CommonMessageDialog(this,CommonMessageDialog.DialogStyle.TIP);
            dialog.setMsg(getString(R.string.phrase_error_prompt));
            dialog.setLeftBtnTxt(getString(R.string.ok));
            dialog.setClickListener(new CustomDialog.CustomDialogOnClickListener() {
                @Override
                public void click(boolean flag) {
                    if (!flag) {
                    }
                }
            });
            dialog.show();
        }
    }

    private boolean verifyMnemonic(){
        if(mVerifyMnemonicList.size()!=12){
            return false;
        }else {
            for(int i=0;i<mVerifyMnemonicList.size();i++){
                if(!mVerifyMnemonicList.get(i).word.equals(mMnemonicBackupList.get(i))){
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 设置选中助记词
     * @param
     * @return
     */
    public boolean setSelection(Mnemonic mnemonic) {
        //之前已选中
        if (mnemonic.isChecked) {
            return false;
        }
        //新增选中
        else {
            mnemonic.isChecked = true;
            mRvMnemonicAdpter.notifyDataSetChanged();

            mVerifyMnemonicList.add(mnemonic);
            mRvVerifyMnemonicAdpter.notifyDataSetChanged();
            return true;
        }
    }


    /**
     * 取消选中助记词
     * @param
     * @return
     */
    public boolean setUnselected(Mnemonic mnemonic) {
        mnemonic.isChecked = false;
        mVerifyMnemonicList.remove(mnemonic);

        mRvMnemonicAdpter.notifyDataSetChanged();
        mRvVerifyMnemonicAdpter.notifyDataSetChanged();
        return true;
    }

    @Override
    protected void onDestroy() {
        dismissLoadingDialog();
        super.onDestroy();
    }
}
